package com.example.fszq.volleydemo1.adapters;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.fszq.volleydemo1.R;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by fszq on 2015/12/24.
 */
public class ImageSelectAdapter extends ArrayAdapter<String> {
    private int resourceId;
    //ImageLoader imageLoader = ImageLoader.getInstance();
    ViewHolder viewHolder;
    String imgSrc = "";

    /**
     * 用户选择的图片，存储为图片的完整路径
     */
    public static List<String> mSelectedImage = new LinkedList<String>();

    /**
     * 文件夹路径
     */
    private String mDirPath;

    public  ImageSelectAdapter(Context context, int ViewResourceId, List<String> objects,String dirPath) {
        super(context, ViewResourceId, objects);
        resourceId = ViewResourceId;
        this.mDirPath = dirPath;
    }


    @Override
    public  View getView(int position, View convertView, ViewGroup parent) {
        View view;
        imgSrc = getItem(position);
        //Log.d("get position", position + "");

        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();

            viewHolder.sImage = (ImageView)view.findViewById(R.id.id_item_image);
            viewHolder.sItemSelected = (ImageView)view.findViewById(R.id.id_item_select);

            //为图片增加点击事件
            /*viewHolder.sImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mSelectedImage.contains(mDirPath + "/" + imgSrc)) {
                        //Log.d("clickimage1", mDirPath + "/" + imgSrc);
                        mSelectedImage.remove(mDirPath + "/" + imgSrc);
                        viewHolder.sItemSelected.setImageResource(R.drawable.picture_unselected);
                        viewHolder.sImage.setColorFilter(null);
                    } else {
                        //Log.d("clickimage2", mDirPath + "/" + imgSrc);
                        mSelectedImage.add(mDirPath + "/" + imgSrc);
                        viewHolder.sItemSelected.setImageResource(R.drawable.pictures_selected);
                        viewHolder.sImage.setColorFilter(Color.parseColor("#77000000"));
                    }

                }
            });*/

            if(mSelectedImage.contains(mDirPath + "/" + imgSrc)) {
                viewHolder.sItemSelected.setImageResource(R.drawable.pictures_selected);
                viewHolder.sImage.setColorFilter(Color.parseColor("#77000000"));
            };

            view.setTag(viewHolder); // 将ViewHolder存储在View中

        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder
        }

        //imageLoader.displayImage("file://" + mDirPath + "/" + imgSrc, viewHolder.sImage);
        Glide.with(getContext())
                .load("file://" + mDirPath + "/" + imgSrc)
                .into(viewHolder.sImage);

        viewHolder.sItemSelected.setImageResource(R.drawable.picture_unselected);


        return view;
    }


    class ViewHolder {
        ImageView sImage,sItemSelected;

    }

}
